package com.yyw.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yyw.Service.ClassService;
import com.yyw.bean.Class;
import com.yyw.bean.User;
import com.yyw.dao.ClassDao;
import com.yyw.dao.UserDao;
import com.yyw.util.MyContext;
import com.yyw.util.Result;
import com.yyw.util.ResultFactory;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Api(tags = "班级API")
@RestController
@RequestMapping("/user")
@Slf4j
public class ClassController {

    @Autowired
    private ClassService classService;

    @Autowired
    private ClassDao classDao;

    @Autowired
    private UserDao userDao;

    // 教师创建班级
    @CrossOrigin
    @ApiOperation(value = "教师创建班级", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "name", value = "班级名称", required = true) })
    @PostMapping("/saveClass")
    public Result saveClass(@RequestParam("name") String name) {
        User user = MyContext.getSessionUser();
        if (user == null) {
            return ResultFactory.notice("未登录");
        }
        return classService.saveClass(new Class(null, name, user.getId(), null, null));
    }

    // 学生根据班级Id加入班级
    @CrossOrigin
    @ApiOperation(value = "学生根据班级Id加入班级", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "classid", value = "班级id", required = true) })
    @PostMapping("/intoClass")
    @Transactional
    public Result intoClass(Long classid) {
        if (classid == null) {
            return ResultFactory.notice("数据不合法");
        }
        User user = MyContext.getSessionUser();
        User newUser = userDao.getUserByTel(user.getTel());
        if (newUser.getClassid() != null) {
            return ResultFactory.notice("只能加入一个班级");
        }
        Class clas = classDao.getClassById(classid);
        if (clas == null) {
            return ResultFactory.notice("班级不存在");
        }

        User user2 = new User();
        user2.setId(newUser.getId());
        user2.setClassid(clas.getId());
        user2.setClassname(clas.getName());

        int i = userDao.updateUser(user2);
        if (i > 0) {
            return ResultFactory.success("加入成功");
        } else {
            return ResultFactory.failure("加入失败");
        }
    }

    // 根据教师id获取所有班级信息
    @CrossOrigin
    @ApiOperation(value = "根据教师id获取所有班级信息", httpMethod = "GET")
    @GetMapping("/getClassByUserId")
    public Result getClassByUserId() {
        User user = MyContext.getSessionUser();
        List<Class> classList = classDao.getClassByUserId(user.getId());

        return ResultFactory.success(classList);
    }

    // 教师解散指定班级
    @CrossOrigin
    @ApiOperation(value = "教师解散指定班级", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "classid", value = "班级id", required = true) })
    @GetMapping("/releaseClass")
    @Transactional
    public Result releaseClass(@RequestParam("classid") Long classid) {
        if (classid == null) {
            return ResultFactory.notice("数据不合法");
        }
        User user = MyContext.getSessionUser();
        int i = classDao.deleteClass(classid);
        if (i > 0) {
            classDao.deleteUserClass(classid, null);
            return ResultFactory.success("解散成功");
        } else {
            return ResultFactory.notice("未找到班级");
        }
    }

    /**
     * 剔除班级中的指定学生
     *
     * @param classid
     * @return
     */
    @CrossOrigin
    @ApiOperation(value = "剔除班级中的指定学生", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "classid", value = "班级id", required = true),
            @ApiImplicitParam(name = "userid", value = "用户id", required = true) })
    @GetMapping("/releaseUser")
    @Transactional
    public Result releaseUser(@RequestParam("classid") Long classid, @RequestParam("userid") Long userid) {
        if (classid == null || userid == null) {
            return ResultFactory.notice("数据不合法");
        }
        int i = classDao.deleteUserClass(classid, userid);
        if (i > 0) {
            return ResultFactory.success("剔除成功");
        } else {
            return ResultFactory.failure("剔除失败");
        }

    }

    @CrossOrigin
    @ApiOperation(value = "获取指定班级学生信息，不分页", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "classid", value = "班级id", required = true) })
    @GetMapping("/getClassInfo")
    public Result getClassInfo(@RequestParam("classid") Long classid) {
        return classService.getUsersByClassId(classid);
    }

    @CrossOrigin
    @ApiOperation(value = "分页获取指定班级的学生信息", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "pageNo", value = "当前页数", required = false),
            @ApiImplicitParam(name = "pageNum", value = "每页数量", required = false),
            @ApiImplicitParam(name = "classid", value = "班级id", required = true),
            @ApiImplicitParam(name = "search", value = "昵称查找", required = false) })
    @GetMapping("/getClassUsers")
    public Result getClassUsers(@RequestParam(value = "pageNo", defaultValue = "1", required = false) Integer pageNo,
            @RequestParam(value = "pageNum", defaultValue = "10", required = false) Integer pageNum,
            @RequestParam(value = "classid", required = false) Long classid,
            @RequestParam(value = "search", required = false) String search) {
        if (classid == null) {
            User user = MyContext.getSessionUser();
            classid = user.getClassid();
            if (classid == null) {
                return ResultFactory.notice("未加入班级");
            }
        }
        return classService.getClassUsers(classid, pageNo, pageNum, search);
    }
}
